/*
 * AlbumTableModel.java
 *
 * Created on June 11, 2005, 1:19 PM
 */
package com.bff.mpd.musicjukebox.lib;

import com.bff.mpd.utils.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.objects.MPDAlbum;
import org.bff.javampd.objects.MPDArtist;
import org.bff.javampd.objects.MPDGenre;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public class AlbumTableModel extends SongTableModel {

    private List<MPDSong> dataList;
    private MPDDatabase database;
    public static final int TITLE = 0;
    public static final int LENGTH = 1;
    public static final int TRACK = 2;
    public static final int YEAR = 3;
    public static final int GENRE = 4;
    private HashMap<String, List<MPDSong>> groupMap;
    private static final String FILE_SEPARATOR = System.getProperty("file.separator");
    public final static String[] columnNames = {
        Utils.getMessage("TM_TITLE"),
        Utils.getMessage("TM_LENGTH"),
        Utils.getMessage("TM_TRACK"),
        Utils.getMessage("TM_YEAR"),
        Utils.getMessage("TM_GENRE")
    };

    public AlbumTableModel() {
        this.database = JukeboxProperties.getInstance().getDatabase();
    }

    /** Creates a new instance of TassTableModel */
    public AlbumTableModel(MPDArtist artist, MPDAlbum album) {
        this();
        try {
            dataList = new ArrayList<MPDSong>(getDatabase().findAlbumByArtist(artist, album));
            if (JukeboxProperties.getInstance().isGroupByFolder()) {
                groupByFolder();
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public AlbumTableModel(MPDAlbum album) {
        this();
        try {
            dataList = new ArrayList<MPDSong>(getDatabase().findAlbum(album));
            if (JukeboxProperties.getInstance().isGroupByFolder()) {
                groupByFolder();
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public AlbumTableModel(MPDGenre genre, MPDAlbum album) {
        this();
        try {
            dataList = new ArrayList<MPDSong>(getDatabase().findAlbumByGenre(genre, album));
            if (JukeboxProperties.getInstance().isGroupByFolder()) {
                groupByFolder();
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public AlbumTableModel(String year, MPDAlbum album) {
        this();
        try {
            dataList = new ArrayList<MPDSong>(getDatabase().findAlbumByYear(year, album));
            if (JukeboxProperties.getInstance().isGroupByFolder()) {
                groupByFolder();
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(AlbumTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public int getRowCount() {
        return (dataList.size());
    }

    @Override
    public Object getValueAt(int row, int col) {
        MPDSong song = dataList.get(row);
        switch (col) {
            case (TITLE):
                return song.getTitle();
            case (LENGTH):
                return song.getLength();
            case (TRACK):
                return song.getTrack();
            case (YEAR):
                return song.getYear();
            case (GENRE):
                return (song.getGenre());
            default:
                return (null);
        }
    }

    @Override
    public Class getColumnClass(int column) {
        if (column == TRACK) {
            return Integer.class;
        } else {
            return super.getColumnClass(column);
        }
    }

    @Override
    public int getColumnCount() {
        return (columnNames.length);
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public MPDSong getSong(int row) {
        return dataList.get(row);
    }

    @Override
    public int getArtistColumnIndex() {
        return (-1);
    }

    @Override
    public int getAlbumColumnIndex() {
        return (-1);
    }

    @Override
    public int getTitleColumnIndex() {
        return (TITLE);
    }

    @Override
    public int getLengthColumnIndex() {
        return (LENGTH);
    }

    @Override
    public int getTrackColumnIndex() {
        return (TRACK);
    }

    @Override
    public int getYearColumnIndex() {
        return (YEAR);
    }

    @Override
    public int getGenreColumnIndex() {
        return (GENRE);
    }

    /**
     * @return the database
     */
    private MPDDatabase getDatabase() {
        return database;
    }

    /**
     * @param database the database to set
     */
    private void setDatabase(MPDDatabase database) {
        this.database = database;
    }

    @Override
    public int getSortColumn() {
        return TRACK;
    }

    public List<String> getGroupList() {
        List<String> list = new ArrayList<String>(groupMap.keySet());
        Collections.sort(list);
        return list;
    }

    public void filterGroup(String directory) {
        if(groupMap.get(directory) != null) {
            this.dataList = groupMap.get(directory);
        }
    }

    private void groupByFolder() {
        groupMap = new HashMap<String, List<MPDSong>>();

        List<String> folders = new ArrayList<String>();
        for (MPDSong song : dataList) {
            int lastIndex = song.getFile().lastIndexOf(FILE_SEPARATOR);
            String folder = song.getFile().substring(0, lastIndex);
            if (!folders.contains(folder)) {
                folders.add(folder);
            }
        }


        if (folders.size() > 1) {
            for (String folder : folders) {
                List<MPDSong> songList = new ArrayList<MPDSong>();
                for (MPDSong song : dataList) {
                    int lastIndex = song.getFile().lastIndexOf(FILE_SEPARATOR);
                    String songFolder = song.getFile().substring(0, lastIndex);
                    if (folder.equals(songFolder)) {
                        songList.add(song);
                    }
                }
                groupMap.put(folder, songList);
            }
        }
    }
}
